const commonUtils = require('../../common/commonUtils');
const {request, showTip, getCurrentUserInfo} = require("../../common/request");
const {uploadImageToQiniu} = require("../../common/qiniu");

//动态计算当前页面的可滚动的高度
function getScrollHeight(){
    // 计算滚动区域的高度：屏幕高度 - 导航栏高度 - 底部安全区域高度
    return  wx.getSystemInfoSync().windowHeight - 250;
}

//初始化数据
function initInfo(_this) {
    getCourseList(_this);
}

// pages/yueke/yueke.js
Page({
    data: {
        scrollHeight:getScrollHeight(),
        currentTab: '0',//选项卡默认
        courseList:{},//课程列表
        modalShowModal: false,//模态窗口的显示状态
        modalTitle:'修改课程信息',
        courseSaveInfo:{
            title: '',
            difficultyLev: 0,
            picPath:''
        },//课程保存或更新信息

        difficultyOptions: ['★☆☆☆', '★★☆☆☆', '★★★☆☆','★★★★☆','★★★★★'],
        selectedDifficultyIndex: 0
    },
    onLoad(options) {
        initInfo(this);
    },
    // 下拉刷新触发时执行
    onPullDownRefresh() {
        wx.showLoading({ title: '刷新中...' });
        initInfo(this);
        wx.stopPullDownRefresh();// 停止刷新动画
        wx.hideLoading();
    },
    changeTab(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            currentTab: index
        });
        getCourseList(this);
    },
    changeStatus:function(e){//下架上架
        const that = this;
        const id = e.currentTarget.dataset.id;
        wx.showModal({
            title: '确认提醒',
            content: '是否确认执行该操作？',
            confirmText: '是',
            cancelText: '否',
            success: (res) => {
                if (res.confirm) {
                    // 点击确认按钮后的处理逻辑
                    request({
                        url:'/yoga_business/wechat/course/changeStatus/'+id,
                        method:'GET',
                        data:{
                        },
                        success(res) {
                            that.onLoad()
                        },
                        fail(msg) {
                            showTip();
                        }
                    });
                }
            }
        });
    },
    goEdit:function(e){//编辑
        const that = this;
        const id = e.currentTarget.dataset.id;
        request({
            url:'/yoga_business/wechat/course/getInfo/'+id,
            method:'GET',
            data:{},
            success(res) {
                if(res.success){
                    that.setData({
                        courseSaveInfo:res.content,
                        modalShowModal: true,
                        selectedDifficultyIndex:res.content['difficultyLev']
                    })
                }else{
                    showTip();
                }
            },
            fail(msg) {
                showTip();
            }
        });
    },
    hideModal() {
        this.setData({
            modalShowModal: false
        });
    },
    //统一处理编辑表单数据值变动
    onInputChange(e) {
        const field = e.currentTarget.dataset.field;
        let value;
        if (e.detail.value!== undefined) {
            if (field === 'difficultyLev') {
                // 对于下拉框，更新索引和对应的数值
                const selectedIndex = e.detail.value;
                this.setData({
                    selectedDifficultyIndex: selectedIndex
                });
                value = selectedIndex;
            } else {
                value = e.detail.value;
            }
        }
        const formData = this.data.courseSaveInfo;
        formData[field] = value;
        this.setData({
            courseSaveInfo: formData
        });
    },
    chooseAttachment() {
        const that = this;
        uploadImageToQiniu(
            (content) => {
                that.setData({
                    'courseSaveInfo.picPath': content
                });
            },
            (errorMsg) => {
                showTip(errorMsg);
            }
        );
    },
    onSave() {
        const that = this;
        const formData = this.data.courseSaveInfo;
        console.log('表单数据:', formData);
        request({
            url:'/yoga_business/wechat/course/save',
            method:'POST',
            data:formData,
            success(res) {
                if(res.success){
                    that.setData({
                        modalShowModal: false
                    });
                    getCourseList(that);
                }else{
                    showTip();
                }
            },
            fail(msg) {
                showTip();
            }
        });
    },
    showAdd:function(){
        const that = this;
        that.setData({
            modalShowModal: true,
            modalTitle:'新增课程',
            courseSaveInfo:{
                title: '',
                difficultyLev: 0,
                picPath:''
            }
        });
    }
})

/**
 * 获取课程
 */
const getCourseList = (_this) => {
    request({
        url:'/yoga_business/wechat/course/list',
        method:'POST',
        data:{
            "status":_this.data.currentTab
        },
        success(res) {
            if(res.success){
                if(res.content){
                    _this.setData({courseList:res.content});
                }else{
                    _this.setData({courseList:null});
                }
            }else{
                showTip();
            }

        },
        fail(msg) {
            showTip();
        }
    });
};